import { 
  AmbientLight,
  AnimationAction,
  AnimationMixer,
  BoxGeometry,
  Clock,
  IcosahedronGeometry,
  Mesh,
  MeshLambertMaterial,
  MeshStandardMaterial,
  PerspectiveCamera,
  PlaneGeometry,
  Scene, 
  SphereGeometry, 
  SpotLight, 
  WebGLRenderer
} from "three";
import stats from '../utils/stats';
// 调用
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GUI } from 'dat.gui';
import { DDSLoader } from "three/examples/jsm/loaders/DDSLoader";

let clock = new Clock();
let scene = new Scene();
let renderer = setRenderer();
let camera = setCamera();
let mixer: AnimationMixer;

setLight();
init();

const trackballControls = new OrbitControls(camera, renderer.domElement);
// 调整场景位置
trackballControls.target.set(0, 3, 0);

render();

function render() {
  stats.update();
  trackballControls.update();

  if (mixer) {
    mixer.update(clock.getDelta())
  }

  requestAnimationFrame(render);
  renderer.render(scene, camera);
}

function setRenderer() {
  const renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 支持阴影生成 
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);

  return renderer
}

function setCamera(): PerspectiveCamera {
  let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
  camera.position.set(0, 20, 40);
  scene.add( camera );

  return camera;
}

// 灯光设置
function setLight() {
  scene.add(new AmbientLight(0x444444));
  scene.add(new AmbientLight(0x343434));
  
  let spotLight = new SpotLight(0xffffff);
  spotLight.position.set(-10, 30, 40);
  spotLight.shadow.mapSize.width = 2048;
  spotLight.shadow.mapSize.height = 2048;
  spotLight.shadow.camera.fov = 15;
  spotLight.decay =2;
  spotLight.penumbra = 0.05;
  spotLight.castShadow = true;

  scene.add(spotLight);
  // scene.add(new SpotLightHelper(spotLight));
}

/** 功能区 */
function init() {
  let textureLoader = new DDSLoader();
  let texture = textureLoader.load(
    '/textures/dds/test-dxt1.dds',
    dds => {
      // console.log(dds)
      // .ignore
    }
  );

  let planeGeom = new PlaneGeometry(60, 40, 1, 1);
  let planeMate = new MeshLambertMaterial({color: 0xffffff});
  const plane = new Mesh(planeGeom, planeMate);
  plane.receiveShadow = true;
  plane.rotation.x = -Math.PI / 2;
  scene.add(plane);

  // 多面几何体
  let polyhedron = new IcosahedronGeometry(8, 0);
  let polyhedronMate = new MeshStandardMaterial({
    metalness: 0.2,
    roughness: 0.7,
    map: texture
  });
  let polyhedronMesh = new Mesh(polyhedron, polyhedronMate);
  polyhedronMesh.castShadow = true;
  polyhedronMesh.position.x = 20;
  polyhedronMesh.position.y = 7;
  scene.add(polyhedronMesh);

  // 添加球
  let sphereGeom = new SphereGeometry(5, 20, 20);
  let sphereMate = new MeshStandardMaterial({
    metalness: 0,
    roughness: 0,
    map: texture
  });
  let sphereMesh = new Mesh(sphereGeom, sphereMate);
  sphereMesh.position.y = 5;
  sphereMesh.castShadow = true;
  scene.add(sphereMesh);

  // 添加矩形
  let cubeGeom = new BoxGeometry(10, 10, 10);
  let cubeMate = new MeshStandardMaterial({
    metalness: 0.2,
    roughness: 0.7,
    map: texture
  });
  let cubeMesh = new Mesh(cubeGeom, cubeMate);
  cubeMesh.position.x = -20;
  cubeMesh.position.y = 5;
  cubeMesh.castShadow = true;
  scene.add(cubeMesh);
}

function addGUI(action: AnimationAction): void {
  const gui = new GUI();
}